﻿using Shop.Base.Entity;
using Shop.Base.Entity.Delete;
using Shop.Base.Entity.Insert;
using Shop.Base.Entity.Update;
using Shop.Base.Interface;
using Shop.Entity;
using Shop.Server;
using Shop.Tools;
using System;
using System.Collections.Generic;
using System.Text;

namespace Shop.Base.Server
{
    public class ShopBaseKeyword : BaseServer<ShopBaseKeywordPo>, IShopBaseKeyword
    {
        public List<ShopBaseKeywordPo> GetPage(string keyword, int isDel,int isShow, int pageIndex, int pageSize, out int total)
        {
            total = 0;
            return db.Queryable<ShopBaseKeywordPo>().WhereIF(!keyword.IsNullOrEmpty(), $" ( KeyCn like @keyword  or KeyEn like @keyword or KeyCh like @keyword)", new { keyword = $"%{keyword}%" })
                .WhereIF(isDel > -1, $" isDel = @isDel ", new { isDel = isDel == 1 })
                .WhereIF(isShow > -1, $" isShow = @isShow ", new { isShow = isShow == 1 })
                .ToPageList(pageIndex, pageSize, ref total);
        }

        public bool Insert(AddShopBaseKeyword t, ComUser com)
        {
            t.CreateId = com.Id;
            t.CreateName = com.UserName;
            var model = db.Queryable<ShopBaseKeywordPo>().Where(x => x.IsDel == false && x.Sort == t.Sort).First();
            if (t.Sort == 0 || model?.Id>0)
            {
                var maxSort = db.Queryable<ShopBaseKeywordPo>().Where(x => x.IsDel == false).Max(x => x.Sort);
                t.Sort = maxSort + 1;
            }
            return db.Insertable(t).ExecuteCommand() > 0;
        }

        public bool Update(UpdateShopBaseKeyword t, ComUser com)
        {
            t.UpdateAt = DateTime.Now;
            t.UpdateId = com.Id;
            t.UpdateName = com.UserName;
            var model = db.Queryable<ShopBaseKeywordPo>().Where(x => x.IsDel == false && x.Sort == t.Sort && x.Id != t.Id).First();
            if (t.Sort == 0 || model?.Id>0)
            {
                var maxSort = db.Queryable<ShopBaseKeywordPo>().Where(x => x.IsDel == false).Max(x => x.Sort);
                t.Sort = maxSort + 1;
            }
            return db.Updateable(t).ExecuteCommand() > 0;
        }
    }
}
